/**********************************************************************
 * SailmiPIM - modular cross-platform feature Personal Information Manager Tools.
 * Copyright(C) 2006-2014  Georg Rudoy
 *
 * Distributed under the Boost Software License, Version 1.0.
 *(See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
 **********************************************************************/

#include "sysstorage.h"
#include <QDir>
#include <QSqlError>
#include <util/db/oral/oral.h>
#include <util/db/dblock.h>
#include <util/db/util.h>
#include <util/sll/prelude.h>
#include <util/sys/paths.h>

namespace SailmiPIM
{
struct SysStorage::UserRecord
{
    Util::oral::Unique<Util::oral::NotNull<QByteArray>> Id_;
    Util::oral::NotNull<QString> UserName_;
    Util::oral::NotNull<QString> NickName_;
    Util::oral::NotNull<QString> UserPass_;
    Util::oral::NotNull<QString> UserMail_;
    Util::oral::NotNull<QString> UserPhone_;
    constexpr static auto ClassName = "User"_ct;
};
}

ORAL_ADAPT_STRUCT(SailmiPIM::SysStorage::UserRecord,
                  Id_,
                  UserName_,
                  NickName_,
                  UserPass_,
                  UserMail_,
                  UserPhone_)

namespace SailmiPIM
{
namespace sph = Util::oral::sph;

SysStorage::SysStorage(QObject *parent)
    : QObject { parent }
    , DB_ { QSqlDatabase::addDatabase("QSQLITE",
                                    Util::GenConnectionName("com.sailmi.pim.Core.SysStorage")) }
{
    const auto& coreDir = Util::GetUserDir(Util::UserDir::SailMI, "");
    DB_.setDatabaseName(coreDir.filePath("sys.db"));

    if(!DB_.open())
    {
        qWarning() << Q_FUNC_INFO
                   << "cannot open the database";
        Util::DBLock::DumpError(DB_.lastError());
        throw std::runtime_error { "Cannot create database" };
    }

    Util::RunTextQuery(DB_, "PRAGMA synchronous = NORMAL;");
    Util::RunTextQuery(DB_, "PRAGMA journal_mode = WAL;");

    UserRecord_ = Util::oral::AdaptPtr<UserRecord>(DB_);
}

SysStorage::~SysStorage() = default;

void SysStorage::AddUser(const Id& id,
                         const QString& userName,
                         const QString& nickName,
                         const QString& userPass,
                         const QString& userMail,
                         const QString& userPhone)
{
    UserRecord_->Insert({ id.toByteArray(), userName , nickName , userPass , userMail ,userPhone });
}

void SysStorage::DeleteUser(const Id& id)
{
    UserRecord_->DeleteBy(sph::f<&UserRecord::Id_> == id.toByteArray());
}

bool SysStorage::loginUser(const QString& userName, const QString& userPass){
    QSqlQuery query;
    query.exec(QString(R"(SELECT count(*) as userCount FROM user WHERE userName_='%1' AND userPass_='%2';)")
                   .arg(userName)
                   .arg(userPass)
               );

    //获取查询结果的第0个值，
    //如果结果是多行数据，可用while(query.next()){}遍历每一行
    //int resultCount=-1;
    //if(query.next()){
    //    resultCount=query.value(0).toInt();
    //}
    //qDebug()<<resultCount;
    //if(resultCount>0){
    //    return true;
    //}else{
    //    return false;
    //}
    return false;
}

}
